﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：IRabbitMqConfiguration.cs
// 功能描述：TongTongMall.RabbitMq
// 
// Create User：jym 2017/02/20
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Dependecy;
using TongTongMall.Extensions;

namespace TongTongMall.RabbitMq.RabbitMq
{
    public interface IRabbitMqConfiguration : ISingletonDependency
    {
        string RabbitMqUrlString { get; }

        string ExchangeName { get; }

        string QueueName { get; }

        bool IsEnabled { get; }
    }

    public class RabbitMqConfiguration : IRabbitMqConfiguration
    {
        private const string RabbitMqUrlStringKey = "TongTongMall.RabbitMq.Url";
        private const string RabbitMqEnabledStringKey = "TongTongMall.RabbitMq.Enabled";



        public string ExchangeName
        {
            get
            {
                return "TongTongMall.Web.CommandMessageExchange";
            }
        }

        public string QueueName
        {
            get
            {
                return "TongTongMall.Web.TextingServiceCommandQueue";
            }
        }

        public bool IsEnabled
        {
            get
            {
                bool isEnabled;
                bool.TryParse(ConfigurationManager.AppSettings[RabbitMqEnabledStringKey], out isEnabled);
                return isEnabled;
            }
        }

        public string RabbitMqUrlString
        {
            get
            {
                var url = ConfigurationManager.AppSettings[RabbitMqUrlStringKey];
                if (url == null || url.IsNullOrWhiteSpace())
                {
                    return @"amqp://localhost";
                }
                return url;
            }
        }
    }
}
